package org.example;

import java.io.File;
import java.nio.file.*;
import java.util.concurrent.*;

public class IOIntensiveExample {
    private static final int THREAD_POOL_SIZE = Runtime.getRuntime().availableProcessors();

    public static void main(String[] args) throws Exception {
        ExecutorService executor = Executors.newFixedThreadPool(THREAD_POOL_SIZE);
        Path filePath = Paths.get(System.getProperty("user.dir")+ File.separator+"asmTest/pom.xml");

        // 读取文件内容并提交任务到线程池
        Files.lines(filePath).forEach(line ->
                executor.submit(() -> processLine(line))
        );

        executor.shutdown();
        executor.awaitTermination(1, TimeUnit.HOURS);
    }

    // 模拟IO密集型操作（如写入数据库、调用API）
    private static void processLine(String line) {
        try {
            // 模拟耗时IO操作（如网络请求）
            Thread.sleep(100);
            System.out.println(Thread.currentThread().getName() + " processed: " + line);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
}